package com.example.test;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.inuker.bluetooth.library.BluetoothClient;
import com.inuker.bluetooth.library.Constants;
import com.inuker.bluetooth.library.beacon.Beacon;
import com.inuker.bluetooth.library.connect.listener.BleConnectStatusListener;
import com.inuker.bluetooth.library.connect.response.BleConnectResponse;
import com.inuker.bluetooth.library.connect.response.BleReadResponse;
import com.inuker.bluetooth.library.connect.response.BleWriteResponse;
import com.inuker.bluetooth.library.model.BleGattProfile;
import com.inuker.bluetooth.library.receiver.listener.BluetoothBondListener;
import com.inuker.bluetooth.library.search.SearchRequest;
import com.inuker.bluetooth.library.search.SearchResult;
import com.inuker.bluetooth.library.search.response.SearchResponse;
import com.inuker.bluetooth.library.utils.BluetoothLog;

import java.util.UUID;

public class BlueToothActivity extends AppCompatActivity {
    BluetoothClient mClient;
    SearchRequest request;
    //        private String MAC = "EC:8C:9A:53:4C:BD";//HUAWEI P20
//    private String MAC = "2C:6E:85:D1:7B:62";//DESKTOP-QFKTI24
    private String MAC = "54:6C:0E:35:78:18";//EDIFIER LolliPods R

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blue);
        mClient = new BluetoothClient(this);
        mClient.registerBluetoothBondListener(mBluetoothBondListener);
        mClient.unregisterBluetoothBondListener(mBluetoothBondListener);
    }

    public void startScan(View view) {
        request = new SearchRequest.Builder()
                .searchBluetoothLeDevice(3000, 3)   // 先扫BLE设备3次，每次3s
                .searchBluetoothClassicDevice(5000) // 再扫经典蓝牙5s
                .searchBluetoothLeDevice(2000)      // 再扫BLE设备2s
                .build();
        mClient.search(request, new SearchResponse() {
            @Override
            public void onSearchStarted() {

            }

            @Override
            public void onDeviceFounded(SearchResult device) {
                Beacon beacon = new Beacon(device.scanRecord);
                if (!TextUtils.isEmpty(device.getName()) && !device.getName().equals("NULL"))
                    BluetoothLog.e("onDeviceFounded:" + device.getName() + device.getAddress());
//                BluetoothLog.v(String.format("beacon for %s\n%s", device.getAddress(), beacon.toString()));
            }

            @Override
            public void onSearchStopped() {

            }

            @Override
            public void onSearchCanceled() {

            }
        });
    }

    public void stopScan(View view) {
        mClient.stopSearch();
    }

    public void connect(View view) {
        mClient.connect(MAC, new BleConnectResponse() {
            @Override
            public void onResponse(int code, BleGattProfile profile) {
                switch (code) {
                    case Constants.REQUEST_SUCCESS:
                        showToast("REQUEST_SUCCESS");
                        break;
                    case Constants.REQUEST_FAILED:
                        showToast("REQUEST_FAILED");
                        break;
                }
            }
        });
        mClient.registerConnectStatusListener(MAC, mBleConnectStatusListener);
    }

    public void disconnect(View view) {
        mClient.disconnect(MAC);
    }

    public void refreshCache(View view) {
        mClient.refreshCache(MAC);
    }

    public void readCharacteristic(View view) {
        mClient.read(MAC, UUID.fromString("6e400001-b5a3-f393-e0a9-e50e24dcca9e"),
                UUID.fromString("6e400002-b5a3-f393-e0a9-e50e24dcca9e"),
                new BleReadResponse() {
                    @Override
                    public void onResponse(int code, byte[] data) {
                        if (code == Constants.REQUEST_SUCCESS) {
                            showToast("data" + data.toString());
                        }
                    }
                });
    }

    public void writeCharacteristic(View view) {
        mClient.write(MAC, UUID.fromString("6e400001-b5a3-f393-e0a9-e50e24dcca9e"),
                UUID.fromString("6e400002-b5a3-f393-e0a9-e50e24dcca9e"), "bytes".getBytes(), new BleWriteResponse() {
                    @Override
                    public void onResponse(int code) {
                        if (code == Constants.REQUEST_SUCCESS) {
                            showToast("code" + code);
                        }
                    }
                });
    }

    public void readDescriptor(View view) {
        mClient.readDescriptor(MAC, UUID.fromString("6e400001-b5a3-f393-e0a9-e50e24dcca9e"),
                UUID.fromString("6e400002-b5a3-f393-e0a9-e50e24dcca9e"),
                UUID.fromString("00002901-0000-1000-8000-00805f9b34fb"), new BleReadResponse() {
                    @Override
                    public void onResponse(int code, byte[] data) {
                        showToast("code" + code);
                    }
                });
    }

    public void writeDescriptor(View view) {
//        mClient.writeDescriptor(MAC, serviceUUID, characterUUID, descriptorUUID, bytes, new BleWriteResponse() {
//            @Override
//            public void onResponse(int code) {
//
//            }
//        });
    }

    private final BleConnectStatusListener mBleConnectStatusListener = new BleConnectStatusListener() {

        @Override
        public void onConnectStatusChanged(String mac, int status) {
            if (status == Constants.STATUS_CONNECTED) {
                showToast("STATUS_CONNECTED");
            } else if (status == Constants.STATUS_DISCONNECTED) {
                showToast("STATUS_DISCONNECTED");
            }
        }
    };

    private final BluetoothBondListener mBluetoothBondListener = new BluetoothBondListener() {
        @Override
        public void onBondStateChanged(String mac, int bondState) {
            // bondState = Constants.BOND_NONE, BOND_BONDING, BOND_BONDED
            switch (bondState) {
                case Constants.BOND_NONE:
                    showToast("BOND_NONE");
                    break;
                case Constants.BOND_BONDING:
                    showToast("BOND_BONDING");
                    break;
                case Constants.BOND_BONDED:
                    showToast("BOND_BONDED");
                    break;
            }
        }
    };

    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
